// Variables: 
char note = 0;            // The MIDI note value to be played
int time = 0;
int BPM = 100;
int greenLedPin = 13;
int redLedPin = 11;
int beatPin = 7;
int blueLedPin = 10;
char drums = 0x9F;
char lead = 0x90;


byte incomingByte;
//  plays a MIDI note.  Doesn't check to see that
//  cmd is greater than 127, or that data values are  less than 127:
void noteOn(char cmd, char data1, char data2) {
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}


void setup() {
  //  Set MIDI baud rate:
  Serial.begin(31250);
  pinMode (beatPin,INPUT);
  digitalWrite(beatPin,HIGH);

  pinMode (greenLedPin,OUTPUT);
  pinMode (blueLedPin,OUTPUT);
  pinMode (redLedPin,OUTPUT);

}

void loop() {
    for (int i = 0; i < Serial.available() ; i = i) {
	incomingByte = Serial.read();

	Serial.print(incomingByte,BYTE);
    }

 
 
   // time = time in beats. 120 bpm = 2 beats every sec. ie time between beats (500). 500/4 beat per cycle = 125. 10 steps per beat = 12.5
   if ( digitalRead(beatPin) == HIGH ){

	    if (time == 0){
		noteOn(drums, 36,0x45);
		noteOn(drums, 46,0x45); }
	
	    if (time == 10) {
		noteOn(drums, 42,0x45); }
	 
	    if (time == 20){
		noteOn(drums, 42,0x00); 
		noteOn(drums, 38,0x50);	}

	    if (time == 25) {
		noteOn(drums, 42,0x45); }

	    if (time == 30){

		noteOn(drums, 42,0x45); }
	
	    if (time == 40){

		noteOn(drums, 42,0x45); }
	
	    if (time == 50){
		noteOn(drums, 36,0x45);
		noteOn(drums, 46,0x45); }

	    if (time == 55){

		noteOn(drums, 42,0x45); }

	    if (time == 60){
		noteOn(drums, 38,0x50);
		noteOn(drums, 42,0x00); }

	    if (time == 70){
		noteOn(drums, 46,0x45); }

	    if (time == 75){
		noteOn(drums, 46,0x45); }

    }

	
    if (time >= 0 && time >= 20){
	digitalWrite(greenLedPin,HIGH);
	digitalWrite(blueLedPin,LOW);
	digitalWrite(redLedPin,LOW);
    }


    if (time > 20 && time > 40){
	digitalWrite(redLedPin,HIGH);
	digitalWrite(greenLedPin,LOW);
	digitalWrite(blueLedPin,LOW);
    }

    if ( 40 < time && time >= 60){
	digitalWrite(blueLedPin,HIGH);
	digitalWrite(greenLedPin,LOW);
	digitalWrite(redLedPin,LOW);
    }

    if ( 60 < time && time >= 80){
	digitalWrite(blueLedPin,HIGH);
	digitalWrite(greenLedPin,LOW);
	digitalWrite(redLedPin,HIGH);
    }

    delay(20);
    time++;
    if (time > 80){
	time = 0;
    }
}


